<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <!-- 默认插槽内容 -->
        <el-tabs>
          <!----------------------- 放置页签 ---------------->
          <el-tab-pane label="角色管理">
            <!-------------------- 新增角色按钮 -------------->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="show = true"
              >
                新增角色
              </el-button>
            </el-row>
            <!----------------------- 表格 渲染列表数据 -------------------->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <!-- <p>{{ row }}</p> -->
                  <el-button
                    size="mini"
                    type="success"
                    plain
                    @click="openAssignPerm(row)"
                  >
                    分配权限
                  </el-button>
                  <el-button size="mini" type="primary" plain>编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    plain
                    @click="delRole(row.id)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--------------------- 分页组件 ------------------>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!------------------- (分页设置) ------------------->
              <el-pagination
                :current-page="query.page"
                :page-size="query.pagesize"
                :total="query.total"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <!-- el-tab-pane 可以添加页签 -->
          <el-tab-pane label="公司信息">
            <h1>公司信息</h1>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!---------------------------- 新增功能弹窗 ------------------------------>
    <el-dialog title="编辑弹层" :visible="show" @close="close">
      <!------------------ 表单 ---------------->
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <!------------------ 角色 ---------------->
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-------------------- 底部按钮 ---------------->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 给角色分配权限点 -->
    <AssignPerm ref="roleDetail" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole } from '@/api/setting'
import AssignPerm from './components/assign-perm.vue'
export default {
  components: {
    AssignPerm
  },
  data () {
    return {
      showAssignDialog: false, // 添加权限点的状态
      show: false, // 弹窗显示隐藏
      list: [], // 角色列表
      // ------------------------- 查询参数 --------------------
      query: {
        page: 1, // 放置页码
        pagesize: 10, // 每页几条
        total: 0 // 记录总数
      },
      // --------------------- 表单数据（新增|编辑） -------------
      roleForm: {
        name: '',
        description: ''
      },
      // ---------------------------- 校验 -----------------------
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },

  // ============ created 钩子函数, 用于调用获取数据的api方法 =============
  created () {
    this.getRoleList()
  },

  methods: {

    // ------------------------ 获取数据 --------------------------------[查]
    async getRoleList () {
      const { total, rows } = await getRoleList(this.query)
      // console.log(total, rows)
      this.list = rows
      this.query.total = total
    },

    // ------------------------ 切换分页 --------------------------------
    changePage (currPage) {
      this.query.page = currPage
      this.getRoleList()
    },

    // ------------------------- 删除角色 --------------------------------[删]
    async delRole (id) {
      /**
       * 1. 用户确认后
       * 2. 调用接口删除
       * 3. 刷新列表
       */
      try {
        await this.$confirm('确认删除吗？')
        // 确定
        await deleteRole(id)
        this.query.page = 1
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        // 取消
        console.log('取消')
      }
    },

    // -------------------- 点击确认按钮 => 进行新增 ----------------------[增]
    async btnOk () {
      /**
       * 1. 重新拉取角色列表，让新增的数据得以显示
       * 2. 提示用户本次操作已成功
       * 3. 关闭弹框
       */
      try {
        await this.$refs.roleForm.validate()
        // 校验通过走这
        await addRole(this.roleForm)
        this.query.page = 1
        this.getRoleList()
        this.show = false
        this.$message.success('添加成功')
      } catch (error) {
        console.log(error)
      }
    },

    // ------------------------- 关闭弹窗执行 -----------------------------
    close () {
      console.log('弹窗关闭')
    },

    // ---------------------- 点击取消按钮 => 重置表单 ---------------------
    btnCancel () {
      /**
        * 1. 清空表单数据
        * 2. 移除校验
        * 3. 关闭弹框
       */
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.show = false
    },

    // 打开弹层
    openAssignPerm (row) {
      this.showAssignDialog = true
      // 父组件调用回显的方法
      this.$refs.roleDetail.showAssign(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
